package com.nikanbujian.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nikanbujian.reggie.common.R;
import com.nikanbujian.reggie.entity.Employee;
import com.nikanbujian.reggie.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.convert.PeriodUnit;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.time.LocalDateTime;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-13
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    IEmployeeService iEmployeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = iEmployeeService.getOne(lambdaQueryWrapper);
        if (one==null){
            return R.error("登录失败");
        }
        if(!one.getPassword().equals(password)){
            return R.error("密码错误");
        }
        if (one.getStatus()==0){
            return R.error("z账号已禁用");
        }
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("推出成功");
    }
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("员工信息",employee);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Long userid = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(userid);
//        employee.setCreateUser(userid);
        iEmployeeService.save(employee);
        return R.success("新增员工成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize ,String name){
        log.info("{},{},{}",page,pageSize,name);
        Page page1 = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        lambdaQueryWrapper.orderByAsc(Employee::getUpdateTime);
        iEmployeeService.page(page1,lambdaQueryWrapper);
        return R.success(page1);
    }

    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        Long employee1 = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
         employee.setUpdateUser(employee1);
        iEmployeeService.updateById(employee);
        return null;
    }


    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("{}",id);
        Employee employee=iEmployeeService.getById(id);
if (employee!=null)
{
    return R.success(employee);
}
        return R.error("查不到0");
    }
}
